package com.bjtech.web.action.management;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bjtech.bo.ShareBO;
import com.bjtech.dao.BaseShareAreaInfoDao;
import com.bjtech.dao.ShareAreaResultDao;
import com.bjtech.dao.ShareSinkResultDao;
import com.bjtech.dao.ShareUserResultDao;
import com.bjtech.domain.BaseShareAreaInfo;
import com.bjtech.domain.ShareAreaResult;
import com.bjtech.domain.query.BaseShareAreaInfoQuerySpec;
import com.bjtech.domain.query.ShareAreaResultQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;

public class ShareAreaResultAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private ShareAreaResultDao shareAreaResultDao;
	private ShareAreaResultQuerySpec selectBean;
	
	private BaseShareAreaInfoDao baseShareAreaInfoDao;
	private List shareAreaList;
	
	private ShareSinkResultDao shareSinkResultDao;
	private List shareSinkResultList;
	
	private String custom;
	
	private ShareBO shareBO;
	
	private ShareUserResultDao shareUserResultDao;
	private List shareUserResultList;
	
	public void prepare() throws Exception {

	}
	
	public ShareAreaResultAction() throws Exception{
		model = new ShareAreaResult();
		selectBean = new ShareAreaResultQuerySpec();
	}
	
	public void setShareAreaResultDao(ShareAreaResultDao shareAreaResultDao) {
		this.shareAreaResultDao = shareAreaResultDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		selectBean.queryParamString();
		shareAreaList = baseShareAreaInfoDao.selectEntityList(new BaseShareAreaInfo(), new BaseShareAreaInfoQuerySpec());
		list = this.shareAreaResultDao.selectEntityPaginatedList(new ShareAreaResult(),selectBean);
		return "list";
	}
	
	public String add(){
		shareAreaList = baseShareAreaInfoDao.selectEntityList(new BaseShareAreaInfo(), new BaseShareAreaInfoQuerySpec());
		custom="ShareAreaResult/add.jsp";
		return "custom";
	}
	
	public String save(){
		ShareAreaResult entity = (ShareAreaResult)model;
		if(entity.getId()==null){
			entity.setShare_man(this.getUser().getUsername());
			shareAreaResultDao.insertEntity(entity);
		}else{
			shareAreaResultDao.updateEntity(entity);
		}
		return "success";
	}
	
	public String edit(){
		shareAreaList = baseShareAreaInfoDao.selectEntityList(new BaseShareAreaInfo(), new BaseShareAreaInfoQuerySpec());
		model = this.shareAreaResultDao.selectEntity(new ShareAreaResult(),id);
		return "edit";
	}

	public String view(){
		model = this.shareAreaResultDao.selectEntity(new ShareAreaResult(),id);
		shareUserResultList = shareUserResultDao.selectShareUserResultListByAreaShareId(id);
		return "view";
	}
	
	public String viewAll(){
		model = this.shareAreaResultDao.selectEntity(new ShareAreaResult(),id);
		shareUserResultList = shareUserResultDao.selectShareUserResultListByAreaShareId(id);
		custom = "ShareAreaResult/viewAll.jsp";
		return "custom";
	}
	
	public String viewAll2(){
		model = this.shareAreaResultDao.selectEntity(new ShareAreaResult(),id);
		shareUserResultList = shareUserResultDao.selectShareUserResultListByAreaShareId(id);
		custom = "ShareAreaResult/viewAll2.jsp";
		return "custom";
	}
	
	public String delete(){
		//model = this.shareAreaResultDao.deleteEntity(new ShareAreaResult(),id);
		this.shareAreaResultDao.deleteAllResult(id);
		return "success";
	}
	
	public String share(){
		//String result = this.shareAreaResultDao.updateSharingResult(id);
		Map param = new HashMap();
		param.put("share_area_result_id", id);
		param.put("share_result", null);
		Integer result = this.shareAreaResultDao.updateSharingResultByProcedure(param);
		if(result!=null && result.intValue()!=1){
			super.renderJavaScript("window.onload=function(){alert('分摊失败!')}");
		}else{
			super.renderJavaScript("window.onload=function(){alert('分摊成功！');location.href='ShareAreaResult!list.action';}");
		}
		return null;
	}
	
	public String beginShare(){
		model = this.shareAreaResultDao.selectEntity(new ShareAreaResult(),id);
		shareBO = shareAreaResultDao.createShareBO(id);
		
		custom="ShareAreaResult/share.jsp";
		return "custom";
	}
	
	public ShareAreaResultQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(ShareAreaResultQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public BaseShareAreaInfoDao getBaseShareAreaInfoDao() {
		return baseShareAreaInfoDao;
	}

	public void setBaseShareAreaInfoDao(BaseShareAreaInfoDao baseShareAreaInfoDao) {
		this.baseShareAreaInfoDao = baseShareAreaInfoDao;
	}

	public List getShareAreaList() {
		return shareAreaList;
	}

	public void setShareAreaList(List shareAreaList) {
		this.shareAreaList = shareAreaList;
	}

	public ShareAreaResultDao getShareAreaResultDao() {
		return shareAreaResultDao;
	}

	public String getCustom() {
		return custom;
	}

	public void setCustom(String custom) {
		this.custom = custom;
	}

	public List getShareSinkResultList() {
		return shareSinkResultList;
	}

	public void setShareSinkResultList(List shareSinkResultList) {
		this.shareSinkResultList = shareSinkResultList;
	}

	public ShareSinkResultDao getShareSinkResultDao() {
		return shareSinkResultDao;
	}

	public void setShareSinkResultDao(ShareSinkResultDao shareSinkResultDao) {
		this.shareSinkResultDao = shareSinkResultDao;
	}

	public ShareBO getShareBO() {
		return shareBO;
	}

	public void setShareBO(ShareBO shareBO) {
		this.shareBO = shareBO;
	}

	public List getShareUserResultList() {
		return shareUserResultList;
	}

	public void setShareUserResultList(List shareUserResultList) {
		this.shareUserResultList = shareUserResultList;
	}

	public ShareUserResultDao getShareUserResultDao() {
		return shareUserResultDao;
	}

	public void setShareUserResultDao(ShareUserResultDao shareUserResultDao) {
		this.shareUserResultDao = shareUserResultDao;
	}
}
